package com.education.module.system.sysLogs;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.education.domain.SysLogs;
import com.education.domain.User;
import com.education.framework.base.BaseServices;
import com.education.framework.constants.LogConstants;
import com.education.framework.dao.IDao;
import com.education.framework.domain.SearchParams;
import com.education.framework.page.Page;
import com.education.framework.session.SessionHelper;
import com.education.framework.util.CommonTools;

@Service
public class SysLogsServices extends BaseServices implements IDao<SysLogs>{

	public boolean insertLog(String logType,String logContent,HttpServletRequest request){
		User user = SessionHelper.getInstance().getUser();
		if(null == user) return false;
		SysLogs obj = new SysLogs();
		obj.setLogType(logType);
		obj.setLogContent(logContent);
		obj.setLogLevel(LogConstants.LOG_LEVEL_INFO); //默认
		obj.setCreateUser(user);
		obj.setAccessIp(CommonTools.getClientIP(request));
		return save(obj) > 0 ? true : false;
	}
	
	public boolean insertLog(String logType,String logContent,String logLevel,HttpServletRequest request){
		User user = SessionHelper.getInstance().getUser();
		if(null == user) return false;
		SysLogs obj = new SysLogs();
		obj.setLogType(logType);
		obj.setLogContent(logContent);
		obj.setCreateUser(user);
		obj.setLogLevel(logLevel);
		obj.setAccessIp(CommonTools.getClientIP(request));
		return save(obj) > 0 ? true : false;
	}
	
	@Override
	public List<SysLogs> find(SearchParams searchParams, Page page) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,log_type,log_content,create_user,create_time,create_user_name,access_ip FROM sys_logs");
		String lp = " where ";
		List<Object> argsList = new ArrayList<Object>();
		if(null != searchParams){
			if(null != searchParams.get("logContent") && !"".equals((String)searchParams.get("logContent"))){
				sql.append(lp).append(" log_content like ? ");
				argsList.add("%" + searchParams.get("logContent") + "%");
				lp = " and ";
			}
		}
		
		sql.append(" order by create_time desc");
		Object[] args = argsList.toArray();
		List<SysLogs> list = dao.query(pageSQL(sql.toString(),page),args,new SysLogsRowmapper());
		page.setTotalItem(findRecordCount("select count(id) as cNum from sys_logs ",new Object[]{}));
		return list;
	}
	
	public List<SysLogs> find() {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,log_type,log_content,create_user,create_time,create_user_name,access_ip FROM sys_logs");
		
		List<SysLogs> list = dao.query(sql.toString(),new SysLogsRowmapper());
		return list;
	}

	@Override
	public int save(SysLogs obj) {
		StringBuffer sql = new StringBuffer(); 
		sql.append("insert into sys_logs ( "); 
		sql.append("log_type,log_content,create_user,create_user_name,access_ip ");  
		sql.append(" ) values(?,?,?,?,?) "); 
		Object[] args = {obj.getLogType(),obj.getLogContent(),obj.getCreateUser().getId(),obj.getCreateUser().getLoginName(),obj.getAccessIp() };
		dao.update(sql.toString(), args);	 
		return dao.queryForInt("SELECT LAST_INSERT_ID()"); 
	}

	@Override
	public SysLogs findForObject(Integer id) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id,log_type,log_content,create_user,create_time,create_user_name,access_ip FROM sys_logs ");
		sql.append(" where id=? ");
		
		Object[] args = {id};
		return dao.queryForObject(sql.toString(),args,new SysLogsRowmapper());
	}

	@Override
	public void update(SysLogs obj) {
		 
 StringBuffer sql = new StringBuffer(); 
 sql.append("update sys_logs "); 
 sql.append("set  "); 
 sql.append("log_type=?,log_content=? where id=?  "); 
 Object[] args = {obj.getLogType(),obj.getLogContent(),obj.getId() };
		 
		 dao.update(sql.toString(), args);
	}

	@Override
	public void delete(Integer id) {
		String sql = "delete from sys_logs where id=?";
		dao.update(sql, new Object[]{id});
	}

	@Override
	public boolean findIsExist(String name) {
		return false;
	}
	
	private class SysLogsRowmapper implements RowMapper<SysLogs> {
		@Override
		public SysLogs mapRow(ResultSet rs, int arg1) throws SQLException {
			SysLogs obj = new SysLogs();
			obj.setId(rs.getInt("id")); 
			obj.setCreateTime(rs.getString("create_time")); 
			User user = new User();
			user.setId(rs.getInt("create_user"));
			user.setLoginName(rs.getString("create_user_name"));
			obj.setCreateUser(user); 
			
			obj.setLogContent(rs.getString("log_content")); 
			obj.setLogType(rs.getString("log_type")); 
			obj.setAccessIp(rs.getString("access_ip"));
			return obj;
		}
	}

}
